package com.kim.logs.core.configurers;

import com.kim.logs.core.builders.EsServiceBuilder;
import com.kim.logs.core.builders.InFileServiceBuilder;
import com.kim.logs.core.builders.JdbcServiceBuilder;
import com.kim.logs.core.builders.LoggerAppenderServiceBuilder;
import com.kim.logs.core.provider.LoggerAppenderConfigurerAdapter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.zxp.esclientrhl.repository.ElasticsearchTemplate;

import javax.sql.DataSource;

@Component
public class LogerAppernderServiceConfigurer implements LoggerAppenderConfigurerAdapter<LoggerAppenderServiceBuilder> {
    private LoggerAppenderServiceBuilder builder;

    public void setBuilder(LoggerAppenderServiceBuilder builder) {
        this.builder = builder;
    }

    public InFileServiceBuilder inFile() {
        InFileServiceBuilder builder = LoggerAppenderServiceBuilder.inFile();
        initBuilder(builder);
        return builder;
    }

    public LoggerAppenderServiceBuilder initBuilder(LoggerAppenderServiceBuilder builder) {
        setBuilder(builder);
        return builder;
    }

    public JdbcServiceBuilder jdbc(JdbcTemplate jdbcTemplate) {
        JdbcServiceBuilder builder = LoggerAppenderServiceBuilder.jdbc(jdbcTemplate);
        initBuilder(builder);
        return builder;
    }

    public JdbcServiceBuilder jdbc(DataSource source) {
        JdbcServiceBuilder builder = LoggerAppenderServiceBuilder.jdbc(source);
        initBuilder(builder);
        return builder;
    }

    /**
     * 新增支持es模式
     */
    public EsServiceBuilder es(ElasticsearchTemplate elasticsearchTemplate) {
        EsServiceBuilder builder = LoggerAppenderServiceBuilder.es(elasticsearchTemplate);
        initBuilder(builder);
        return builder;
    }

    /**
     * 外部调用者使用，链式的做法
     *
     * @return 返回对应外部调用者
     */
    @Override
    public LoggerAppenderServiceBuilder and() {
        return getBuilder();
    }

    /**
     * 获取构建器
     *
     * @return 构建器
     */
    @Override
    public LoggerAppenderServiceBuilder getBuilder() {
        return builder;
    }
}
